package com.zhiwei.project.controller;

import com.zhiwei.common.constant.ErrorEnum;
import com.zhiwei.common.constant.ResultResponseMsg;
import com.zhiwei.common.dto.PageDataDto;
import com.zhiwei.common.exception.BasePublishException;
import com.zhiwei.common.util.BeanConverterUtil;
import com.zhiwei.common.util.PageDataUtil;
import com.zhiwei.project.controller.vo.DictionaryQueryVo;
import com.zhiwei.project.controller.vo.DictionaryVo;
import com.zhiwei.project.dto.DictionaryDto;
import com.zhiwei.project.dto.DictionaryQueryDto;
import com.zhiwei.project.service.ProjectDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ZHIWEI.YANG
 * @createtime 2019/11/10 - 16:37
 * @decription 发版系统数据字典控制器
 */
@Api(description = "发版系统数据字典API")
@RestController
@RequestMapping("/dictionary")
@Slf4j
public class ProjectDictController {

    @Autowired
    private ProjectDictService projectDictService;

    /**
     * 获取数据字典
     *
     * @param parentType 类型编码
     * @return
     */
    @GetMapping("/queryByType")
    @ApiOperation(value = "/queryByType", notes = "查询数据字典")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "parentType", value = "数据字典类型编码", paramType = "query", dataType = "String", required = true)
            }
    )
    public ResultResponseMsg queryByType(@RequestParam("parentType") String parentType) throws InterruptedException {
        if(StringUtils.isEmpty(parentType)){
            throw new BasePublishException(ErrorEnum.PARAM);
        }
        List<DictionaryDto> dictionaryList = projectDictService.queryPublishDictByTypeCode(parentType);
        List<DictionaryVo> dictionaryVos = BeanConverterUtil.convertToBeanList(dictionaryList, DictionaryVo.class);
        log.info("查询发版系统数据字典列表 parentTypeCode:{},dictionaryList:{}", parentType, dictionaryVos);
        return ResultResponseMsg.buildSuccessResultMsg(dictionaryVos);
    }

    /**
     * 获取数据字典
     *
     * @param dictionaryQueryVo 请求VO
     * @return
     */
    @PostMapping("/queryByPage")
    @ApiOperation(value = "/queryByPage", notes = "查询数据字典")
    public ResultResponseMsg queryByPage(@RequestBody DictionaryQueryVo dictionaryQueryVo) {
        DictionaryQueryDto dictionaryQueryDto = BeanConverterUtil.convertToBean(dictionaryQueryVo, DictionaryQueryDto.class);
        PageDataDto<DictionaryDto> pageDataDto = projectDictService.queryPublishDictByPage(dictionaryQueryDto);
        return ResultResponseMsg.buildSuccessResultMsg(PageDataUtil.convertPageDtoToVo(pageDataDto, DictionaryVo.class));
    }

    /**
     * 新增数据字典
     * @param dictionaryVo 请求VO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "/add", notes = "新增数据字典")
    public ResultResponseMsg add(@RequestBody DictionaryVo dictionaryVo) {
        DictionaryDto dictionaryDto = BeanConverterUtil.convertToBean(dictionaryVo, DictionaryDto.class);
        boolean result = projectDictService.addDictionary(dictionaryDto);
        log.info("新数据字典请求参数dictionaryVo:[{}], 结果：{}", dictionaryVo, result);
        return result ? ResultResponseMsg.buildSuccessResultMsg() : ResultResponseMsg.buildFailureResultMsg();
    }

    /**
     * 更新数据字典
     * @param dictionaryVo 请求VO
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "/update", notes = "更新数据字典")
    public ResultResponseMsg update(@RequestBody DictionaryVo dictionaryVo) {
        DictionaryDto dictionaryDto = BeanConverterUtil.convertToBean(dictionaryVo, DictionaryDto.class);
        boolean result = projectDictService.updateDictionary(dictionaryDto);
        return result ? ResultResponseMsg.buildSuccessResultMsg() : ResultResponseMsg.buildFailureResultMsg();
    }
}
